<template>
  <div v-loading="loading">
    <div>
      <instance-search v-model="params" :show-finished="false" :show-keyword="false" :show-start-times="false"
        style="width:400px;display: inline-block" />
      <el-input style="width: 200px;margin-right: 20px" prefix-icon="search" :placeholder="$t('webAuto.LCInstanceNumber')" clearable
        v-model="params.processInstanceId" />
      <!-- <el-input  style="width: 200px;margin-right: 20px" prefix-icon="search"
              placeholder="申请单号"
              clearable v-model="params.businessKey"/> -->
    </div>
    <el-table :data="dataList" :header-cell-style="{ background: '#e8e8e8' }" style="width: 100%; margin: 20px 0"
      @row-dblclick="showProcess">
      <el-table-column fixed prop="processDefName" :label="$t('webAuto.ProcessType')" show-overflow-tooltip>
        <template v-slot="scope">
          <el-tag size="small" type="success" v-if="scope.row.superInstanceId !== scope.row.instanceId">{{$t('webAuto.son')}}</el-tag>
          <span style="margin-left: 5px">{{ scope.row.processDefName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="businessKey" :label="$t('webAuto.RequestNo')" min-width="110px"></el-table-column>
      <el-table-column prop="instanceId" :label="$t('webAuto.LCInstanceNumber')" min-width="130px"></el-table-column>
      <el-table-column prop="formAbstracts" show-overflow-tooltip :label="$t('webAuto.zhaiyaoxingxi')" min-width="110px">
        <template v-slot="scope">
          <form-abstracts :value="scope.row.formAbstracts" />
        </template>
      </el-table-column>
      <el-table-column prop="owner" show-overflow-tooltip :label="$t('webAuto.promoter')" min-width="100px">
        <template v-slot="scope">
          <w-avatar :size="35" :name="scope.row.owner.name" :src="scope.row.owner.avatar" />
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="taskName" :label="$t('webAuto.RWjeidian')" />
      <el-table-column show-overflow-tooltip prop="taskCreateTime" :label="$t('webAuto.RWkaishiTime')" min-width="120px"></el-table-column>
      <el-table-column show-overflow-tooltip prop="taskEndTime" :label="$t('webAuto.RWwangchengTime')" min-width="120px"></el-table-column>
      <el-table-column show-overflow-tooltip prop="duration" :label="$t('webAuto.chulihaoshi')" min-width="120px">
        <template v-slot="scope">
          {{ getDuration(scope.row) }}
        </template>
      </el-table-column>
      <el-table-column prop="status" :label="$t('webAuto.ProcessingResults')">
        <template v-slot="scope">
          <el-tag :type="getTaskResult({ result: scope.row.taskResult, finishTime: scope.row.taskEndTime }).type">
            {{ getTaskResult({ result: scope.row.taskResult, finishTime: scope.row.taskEndTime }).text }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('webAuto.operate')" min-width="60">
        <template v-slot="scope">
          <el-button type="warning" text bg size="small" @click="copyClick(scope.row)">{{$t('title.copy')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align: right">
      <el-pagination hide-on-single-page background layout="prev, pager, next" :total="total"
        :page-size="params.pageSize" v-model:current-page="params.pageNo"></el-pagination>
    </div>
    <el-dialog direction="rtl" :title="$t('webAuto.ApprovalDetails')" v-model="processVisible" closeFree
      style="height: auto;min-height:100%;;width: 100%;margin:0!important;" :close-on-click-modal="false"
      :close-on-press-escape="false">
      <instance-preview v-if="processVisible" :instance-id="selectInstance.instanceId"
        @handler-after="handlerAfter"></instance-preview>
    </el-dialog>

    <w-dialog v-if="!isMobile" :title="`${$t('webAuto.initiate')} - ${selectInstance.processDefName}`" closeFree width="1000px"
      v-model="openItemDl" :okText="$t('webAuto.Submit')" :cancelText="$t('webAuto.cancel')" @cancel="openItemDl = false" @ok="submitForm">
      <initiate-process ref="processForm" :node-id="selectInstance.nodeId" :instance-id="selectInstance.instanceId"
        :code="selectInstance.formId" v-if="openItemDl" @ok="openItemDl = false"></initiate-process>
    </w-dialog>

    <!-- 抄送弹框 -->
    <el-dialog v-if="openItemCopy" :title="$t('webAuto.CCProcess')" closeFree width="40%" v-model="openItemCopy">
      <div style="margin-bottom: 10px">
        <span style="margin:20xp 0">{{$t('webAuto.shouldCopy')}}：</span>
        <el-button style="margin-right: 20px" size="small" icon="user" @click="selectUser">{{$t('module.selectPeoples')}}</el-button>
        <el-tag size="small" v-for="(item, index) in orgPicker.value" :key="index" style="margin-right: 10px">{{ item.name
          }}</el-tag>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="openItemCopy = false">{{$t('webAuto.cancel')}}</el-button>
          <el-button type="primary" @click="openItemCopyClick">
            {{$t('webAuto.sure')}}
          </el-button>
        </div>
      </template>
    </el-dialog>
    <org-picker :pc-mode="!isMobile" :title="orgPicker.title" type="user" ref="orgPicker" :multiple="orgPicker.multiple"
      :selected="orgPicker.selected" @ok="selected" />
  </div>
</template>

<script>
import taskApi from '@/api/processTask'
import moment from 'moment'
import OrgPicker from '@/components/common/OrgPicker.vue'
import InstancePreview from '../approval/ProcessInstancePreview2.vue'
import InitiateProcess from '../InitiateProcess.vue'
import { getTaskResult } from "@/utils/ProcessUtil.js";
import FormAbstracts from "./FormAbstracts.vue";
import InstanceSearch from "../../common/InstanceSearch.vue";

export default {
  name: 'Finished',
  components: { InstancePreview, InitiateProcess, FormAbstracts, InstanceSearch, OrgPicker },
  data() {
    return {
      total: 0,
      params: {
        pageSize: 10,
        pageNo: 1,
        finished: null,
        code: '',
        startTimes: [],
        processInstanceId: ''
      },
      openItemDl: false,
      openItemCopy: false,
      selectInstance: {},
      loading: false,
      processVisible: false,
      formList: [],
      dataList: [],
      copyPicker: false,
      rowLi: {},
      orgPicker: {
        multiple: false,
        selected: [],
        title: '选择人员',
        value: []
      },
    }
  },
  computed: {
    isMobile() {
      return window.screen.width < 450
    },
  },
  mounted() {
    this.getIdoList()
  },
  methods: {
    getTaskResult,
    getIdoList() {
      this.loading = true
      taskApi
        .getIdoList(this.params)
        .then((rsp) => {
          this.loading = false
          this.total = rsp.data.total
          this.dataList = rsp.data.records
        })
        .catch((e) => {
          this.loading = false
          this.$router.push('/login')
        })
    },
    reSubmit(row) {
      this.selectInstance = row
      this.openItemDl = true
      if (this.isMobile) {
        this.$router.push(
          `/mbinitiate?code=${row.formId}&instanceId=${row.instanceId}`
        )
      }
    },
    submitForm() {
      this.$refs.processForm.validate((validForm, validProcess) => {
        if (!this.isMobile) {
          if (validForm && validProcess) {
            this.$refs.processForm.submit()
          } else {
            this.$message.warning(this.$t('webAuto.refineOptions'))
          }
        }
      })
    },
    showProcess(row) {
      this.processVisible = true
      this.selectInstance = row
    },
    getDuration(row) {
      let end = this.$isNotEmpty(row.taskEndTime)
        ? row.taskEndTime
        : moment().format('YYYY-MM-DD HH:mm:ss')
      return this.$timeCoverStr(row.createTime, end)
    },
    handlerAfter() {
      this.processVisible = false
      this.getIdoList()
    },
    // 抄送
    copyClick(row) {
      this.rowLi = row
      this.openItemCopy = true
      this.orgPicker.value = []
    },
    // 选择的人员的打开弹框
    selectUser() {
      this.orgPicker.title = this.$t('webAuto.CCRecipients')
      this.orgPicker.multiple = true
      this.copyPicker = true
      this.$refs.orgPicker.show()
    },
    // 选择的人员的确定
    selected(users) {
      this.orgPicker.value.length = 0
      this.orgPicker.value.push(...users)
    },
    // 弹框确定
    openItemCopyClick() {
      if (this.orgPicker.value.length > 0) {
        let ccArr = []
        this.orgPicker.value.forEach(itme => {
          ccArr.push(itme.id)
        })
        taskApi.processCopy({
          userId: this.$store.state.loginUser.id,
          instanceId: this.rowLi.instanceId,
          nodeId: this.rowLi.taskDefKey,
          nodeName: this.rowLi.taskName,
          ccUsers: ccArr,
        }).then(rsp => {
          this.$message.success(this.$t('webAuto.CCSuccessful'))
          this.openItemCopy = false
        })
      } else {
        this.$message.warning(this.$t('webAuto.CCPersonnel'))
      }
    }
  },
  watch: {
    params: {
      deep: true,
      handler() {
        this.getIdoList()
      },
    },
  },
}
</script>

<style scoped></style>
